/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo1;

import Modelo.DAL.ConexionBD;
import Modelo.DAL.PuestoVigilancia_DAO;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author andres
 */
public class listaPuestoVigAction extends org.apache.struts.action.Action {

  /* forward name="success" path="" */
  private static final String SUCCESS = "success";
  private static final String FAILURE = "failure";

  /**
   * This is the action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @throws java.lang.Exception
   * @return
   */
    @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

    String id_puestoVig = request.getParameter("id_puestoVig");
    String nombrePuesto = request.getParameter("nombrePuesto");
    String id_area = request.getParameter("id_area");
    String sede = request.getParameter("sede");
    String offset = request.getParameter("offset");
    String limit = request.getParameter("limit");

    String where = "SELECT p.id_puesto_vigilancia, p.puesto,a.descripcion,p.nombre_sede, e.coordx,e.coordy,estado_puesto,(Select count(*) from puesto_vigilancia where 1 = 1) FROM puesto_vigilancia p,espacio e WHERE p.puesto = e.nombre_espacio AND p.id_area = e.id_area AND p.id_area = a.id_area AND p.nombre_sede = e.nombre_sede";

    if (id_puestoVig != null && !id_puestoVig.equals("")) {
      where += " and id_puesto_vigilancia = '" + id_puestoVig + "'";
    }

    if (nombrePuesto != null && !nombrePuesto.equals("")) {
      where += " and p.puesto = '" + nombrePuesto + "'";
    }

    if (id_area != null && !id_area.equals("")) {
      where += " and p.id_area = '" + id_area + "'";
    }

    if (sede != null && !sede.equals("")) {
      where += " and p.nombre_sede = '" + sede + "'";
    }

    
    where += " AND estado_puesto = 'on'";
    where += " ORDER BY puesto;";
    if (limit != null && !limit.equals("0")) {
      where += " limit " + limit + " offset " + offset;
    }
    response.setContentType("text/json");
    JSONObject ext = new JSONObject();
        PuestoVigilancia_DAO pvDao = new PuestoVigilancia_DAO();
    ext.put("consultarPuestoVig", pvDao.consultarPuestoVigTabla(where));
    PrintWriter pw = response.getWriter();
    pw.write(ext.toString());
    pw.flush();
    pw.close();

    return mapping.findForward(SUCCESS);
  }
}

